package com.aispace.taichu.modules.controller;

import com.baomidou.mybatisplus.extension.service.IService;
import com.aispace.taichu.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公共CURD 抽象工具类
 * @param <I>
 * @param <Entity>
 * @author iron.guo
 * @since 2023.02.22
 * @version 1.0
 */
@SuppressWarnings("ALL")
public class BaseController<I extends IService,Entity> {

    @Autowired
    protected I service;


    /**
     * 通用获取所有数据接口
     * @return
     */
    @GetMapping("/getAll")
    public ResponseResult<List<Entity>> getAll() {
        return ResponseResult.success(service.list());
    }

    /**
     * 数据新增接口
     * @param entity
     * @return {@link ResponseResult<Boolean> }
     */
    @PostMapping("add")
    public ResponseResult<Boolean> add(@RequestBody Entity entity) throws Exception {
        return ResponseResult.success(service.save(entity));
    }

    /**
     * 根据id获取数据对象
     * @param id
     * @return {@link ResponseResult<Entity>}
     */
    @GetMapping("queryById")
    public ResponseResult<Entity> queryById(@RequestParam Long id){
        return (ResponseResult<Entity>) ResponseResult.success(service.getById(id));
    }

    /**
     * 更新数据
     * @param entity
     * @return {@link ResponseResult<Boolean>}
     */
    @PostMapping("edit")
    public ResponseResult<Boolean> edit(@RequestBody Entity entity){
        return ResponseResult.success(service.updateById(entity));
    }

    /**
     * 根据id删除数据
     * @param id
     * @return {@link ResponseResult<Boolean>}
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Boolean> delete(@PathVariable Long id) {
        return ResponseResult.success(service.removeById(id));
    }

    /**
     * 批量删除数据
     * @param ids
     * @return {@link ResponseResult<Boolean>}
     */
    @DeleteMapping("/batchRemove/{ids}")
    public ResponseResult<Boolean> batchRemove(@PathVariable List<Long> ids) {
        return ResponseResult.success(service.removeBatchByIds(ids));
    }

}
